package com.example.xianzhi.controller.API;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.xianzhi.entity.TbUser;
import com.example.xianzhi.service.UserSercive;
import com.example.xianzhi.service.impl.UserSerciveImpl;
import org.lanqiao.mvc.entity.YockMvcAnnotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@YockMvcAnnotation.Controller("/api_wx")
public class API_WxAuthController {
    private UserSercive userSercive = new UserSerciveImpl();

    /*
    http://yesenglish.vip/【项目名】/api_wx/index.do
    >>>>
    http://yesenglish.vip/happyManage-1.0-SNAPSHOT/api_wx/index.do
    http://yesenglish.vip/xianzhi/api_wx/index.do


   https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaa3c9d89ef9e0f64&redirect_uri=【更新此处】&response_type=code&scope=snsapi_userinfo&state=xxx#wechat_redirect
   >>>>>
   https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaa3c9d89ef9e0f64&redirect_uri=http://yesenglish.vip/happyManage-1.0-SNAPSHOT/api_wx/index.do&response_type=code&scope=snsapi_userinfo&state=xxx#wechat_redirect

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaa3c9d89ef9e0f64&redirect_uri=http://yesenglish.vip/xianzhi-1.0-SNAPSHOT/api_wx/index.do&response_type=code&scope=snsapi_userinfo&state=xxx#wechat_redirect
     */

    //ModelAndView  请求转发（内部资源）
    //String        响应内容（一般适用于返回json字符串）
    //void          什么都不做！！


    @YockMvcAnnotation.RequestMapping("/index.do")
    public void index(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws  Exception{

        System.out.println(">>>>>>>>微信来调我了>>>>>>>>>");

        String userJson = (String) request.getAttribute("userJson");
        JSONObject jsonObj  = JSONUtil.parseObj(userJson);
        String openid = jsonObj.getStr("openid");               //微信的唯一的用户ID
        String sex = jsonObj.getStr("sex");                     //性别
        String nickname = jsonObj.getStr("nickname");           //昵称
        String headimgurl = jsonObj.getStr("headimgurl");       //头像

        //////////////////////此处省略存入数据库的步骤//////////////////////
//        String openid = "xxxxx";               //微信的唯一的用户ID
//        String nickname = "test11";           //昵称
//        String headimgurl = "1.jpg";       //头像

        //////////////////////此处省略存入数据库的步骤//////////////////////
        //使用openid当条件去查询用户表有没有这条记录？？？
        //select * from tb_user where open_id = ?
        //userSercive.selectUser(openid);
        //如果没有  ->  就相当于注册（insert语句） ->  取出自增长的用户ID，传给前端
        //如果有   ->  就把用户ID取出来，传给前端..
        TbUser a = null;
        if(userSercive.selectUser(openid)==null){
            userSercive.insertUser(openid,nickname,headimgurl,"","",100,100,"");
            System.out.println(">>>>>>>1");
            a = userSercive.selectUser(openid);
        }else {
            System.out.println(">>>>>>>>2");
            a = userSercive.selectUser(openid);
        }
        System.out.println(">>>>>"+a);
        //重定向
        response.setContentType("text/html; charset=UTF-8");
        String 前端地址="http://baidu.com";
        response.getWriter().print("<script>location.href='"+前端地址+"';</script>");
    }


}
